
  class ListNode {
      int val;
      ListNode next;

      ListNode() {
      }

      ListNode(int val) {
          this.val = val;
      }

      ListNode(int val, ListNode next) {
          this.val = val;
          this.next = next;
      }
  }


class Solution {
    public ListNode rotateRight(ListNode head, int k) {
        if(head == null || head.next == null){
            return head;
        }
        ListNode cur = head;
        int len = 1;//链表长度
        while(cur.next != null){
            len ++;
            cur = cur.next;
        }
        int ret = len -  k % len;//链表元素需要移动的位置
        cur.next = head;//将链表第一个元素和最后一个元素连起来构成环形链表
        while(ret != 0){
            cur = cur.next;
            ret --;
        }
        head = cur.next;
        cur.next = null;

        return head;
    }
}